package com.ruoyi.quality.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.sql.QueryHelp;
import com.ruoyi.common.security.utils.SecurityUtils;

import com.ruoyi.quality.domain.Sampling;
import com.ruoyi.quality.domain.SamplingItem;
import com.ruoyi.quality.domain.dto.SamplingDto;
import com.ruoyi.quality.domain.vo.SamplingVo;
import com.ruoyi.quality.service.SamplingService;
import com.ruoyi.quality.mapper.SamplingMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;

/**
 *
 */
@Service
public class SamplingServiceImpl implements SamplingService{

    @Resource
    SamplingMapper samplingMapper;

    @Override
    public Page<SamplingVo> getSamplingByPage(SamplingDto pageEntity) {
        MPJLambdaWrapper<Sampling> wrapper = new MPJLambdaWrapper<Sampling>()
                .selectAll(Sampling.class)//查询user表全部字段
                //记的测试阶段
                .eq(Sampling::getDelFlag, 0)
                .orderByDesc(Sampling::getCreateTime);
        wrapper = QueryHelp.getPredicateEx(wrapper, Sampling.class, pageEntity);
        Page<SamplingVo> userList = samplingMapper.selectJoinPage(new Page<>(pageEntity.getCurrentPage(), pageEntity.getPageSize()),   SamplingVo.class, wrapper);
        return userList;
    }

    @Override
    public void addSampling(SamplingDto testDto) throws ParseException {
        String userName = SecurityUtils.getUsername();
        MPJLambdaWrapper<Sampling> wrapper = new MPJLambdaWrapper<Sampling>()
                .eq(Sampling::getCode, testDto.getCode())
                .ne(Sampling::getDelFlag, 1);

//        wrapper   = wrapper.eq(Sampling::getCode, testDto.getCode());
//        wrapper.eq(Sampling::getDelFlag, 1);
        Sampling material = samplingMapper.selectOne(wrapper);
        if(null != material) {
            throw new GlobalException("该抽样方案已经存在");
        }
        Sampling test2 = new Sampling();
        BeanUtils.copyProperties(testDto, test2, new String[]{"id"});
        test2.setDelFlag(false);
        test2.setCreateBy(userName);
        test2.setCreateTime(new Date());
        samplingMapper.insert(test2);
    }

    @Override
    public void updateSampling(SamplingDto testDto) {
        String userName = SecurityUtils.getUsername();
        MPJLambdaWrapper<Sampling> wrapper = new MPJLambdaWrapper<Sampling>()
             ;
        Sampling sampling = samplingMapper.selectOne(wrapper);
        if(null == sampling) {
            throw new GlobalException("该Sampling不存在");
        }
        BeanUtils.copyProperties(testDto, sampling);
        sampling.setUpdateBy(userName);
        sampling.setUpdateTime(new Date());
        samplingMapper.updateById(sampling);
    }

    @Override
    public void delSampling(SamplingDto testDto) {
        Sampling material = samplingMapper.selectById(testDto.getId());
        if(null == material) {
            throw new GlobalException("该Sampling不存在");
        }
        material.setDelFlag(true);
        samplingMapper.updateById(material);
    }
}




